﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace sow
{
	/// <summary>
	/// Summary description for emailcompose.
	/// </summary>
	public partial class emailcompose : System.Web.UI.Page
	{
		int UserID;

	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			if(Request["postid"] == null || Request["postid"].ToString() == "")
				Response.Redirect("index.aspx");

			HttpCookie emailCookie = Request.Cookies["Email"];
			if(emailCookie == null)
				Response.Redirect("index.aspx");

			DataSet ds = EmailDataAccess.GetUser(emailCookie.Value);
			UserID = Convert.ToInt32(ds.Tables[0].Rows[0]["UserID"]);

			FCKeditor2.ToolbarSet = "Basic";
			// Put user code to initialize the page here
			if (!IsPostBack) 
			{
				//this.Button1.Attributes["onclick"] = "location.href='emaillist.aspx';return false;";
				//this.Button3.Attributes["onclick"] = "history.go(-1);return false;";
				this.TextBox1.Text = emailCookie.Value;
				ds = EmailDataAccess.GetEmailListByUserID(UserID);
				ListItem listitem = new ListItem("请选择邮件列表", "");
				this.DropDownList1.Items.Add(listitem);
				foreach(DataRow dr in ds.Tables[0].Rows)
				{
					ListItem item = new ListItem(dr["ListName"].ToString(), dr["EmailListID"].ToString());
					this.DropDownList1.Items.Add(item);
				}

				ds = EmailDataAccess.GetDataSet("v_Articles", "PostID=" + Request["postid"].ToString());
				this.Label_Subject.Text = ds.Tables[0].Rows[0]["Subject"].ToString();

				string http = "http://www.springofwater.net/springofwater/client/bloggercontent.aspx?" + "PostID=" + Request["postid"].ToString();
				FCKeditor2.Value = "请点击文章：<a href='" + http + "' target=_blank>" + http + "</a> <br>";
			    FCKeditor2.Value += "<br><br>" + ds.Tables[0].Rows[0]["Summary"].ToString();
                FCKeditor2.Value += "...<br><br><br>谢谢阅读，请传给你的朋友。";

		        string userName = SpringWaterDataAccess.UserObject.RetrieveUserInfo(emailCookie.Value).NickName;
                FCKeditor2.Value += "<br><br>- " + userName;
			}

		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    

		}
		#endregion

		private void ImageButton1_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			 
		}

		protected void Button2_Click(object sender, System.EventArgs e)
		{
			if(this.TextBox2.Text.Trim() == "" && this.DropDownList1.SelectedValue == "")
			{
				Label_Message.Text = "请输入电子邮件或选择电子邮件列表！";
				this.Label_TextBoxName.Text = "TextBox2";
				return;
			}

			string swimage = "<br><br><a title='请点击进入泉源网' href='http://www.springofwater.net/' target=_blank><img border=0 src='http://www.springofwater.net/springofwater/client/images/email_footer.gif'></a>";

			if(this.TextBox2.Text.Trim() != "")
			{
				EmailDataAccess.InsertQueuedEmail(UserID, this.TextBox1.Text.Trim(), this.TextBox2.Text.Trim(),
					this.Label_Subject.Text, FCKeditor2.Value + swimage);
			}
			else
			{
                DataSet ds = EmailDataAccess.GetEmailAddressByEmailListID(Convert.ToInt32(this.DropDownList1.SelectedValue));
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    EmailDataAccess.InsertQueuedEmail(UserID, this.TextBox1.Text.Trim(), dr["EmailAddress"].ToString(),
                        this.Label_Subject.Text, FCKeditor2.Value + swimage);
                }
			}

			Label_Message.Text = "邮件发送成功！";

			//Response.Redirect("bloggercontent.aspx?postid=" + Request["postid"]);
			
		}

		protected void ButtonTop_Click1(object sender, EventArgs e)
        {
            Button2_Click(sender, e);
        }

        protected void Button2_Click1(object sender, EventArgs e)
        {
            Button2_Click(sender, e);
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
           Response.Redirect("bloggercontent.aspx?" + "PostID=" + Request["postid"].ToString());
        }

		}
}
